package Screen
{	
	import Button.UglyButton;
	import Button.UglyTextField;
	import Const.ButtonIDs;	
	import InfoGroup.TrainInputData;
	import Global.GlobalClass;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public final class TrainScreen extends GenericScreen
	{		
		private var cargoTrainData : TrainInputData;
		private var passengerTrainData : TrainInputData;
		
		private var saveButton : UglyButton;		
		
		public function TrainScreen()
		{
			super();
			
			var POSY : int = 50;
			
			cargoTrainData = new TrainInputData("Carga");
			cargoTrainData.x = 100;
			cargoTrainData.y = POSY;
			
			passengerTrainData = new TrainInputData("Pasajeros");
			passengerTrainData.x = 500;
			passengerTrainData.y = POSY;
			
			addChild(cargoTrainData);
			addChild(passengerTrainData);
			
			saveButton = new UglyButton("Guardar", ButtonIDs.BUTTONID_SAVE);
			saveButton.setPos(320, 400);
			saveButton.addEventListener(MouseEvent.CLICK, saveInput);
			
			addChild(saveButton);
			
			initTrains();
		}
		
		private function saveInput(evt:MouseEvent) : void
		{
			trace("GUARDANDO DATOS DE LOS TRENES");
			
			//Save cargo train data
			//La velocidad se setea en km/h pero internamente la toma en m/s para manejar una misma unidad
			//en todas las ctas. Divido por 3.6 para pasar de KM/H a M/S
			GlobalClass.cargoTrain.trainVMax = cargoTrainData.vMax.getTextAsNumber() / 3.6;
			GlobalClass.cargoTrain.accel = cargoTrainData.acel.getTextAsNumber();
			GlobalClass.cargoTrain.brakeAccel = (cargoTrainData.aFren.getTextAsNumber() < 0 ? cargoTrainData.aFren.getTextAsNumber() : cargoTrainData.aFren.getTextAsNumber()*(-1)) ; //Must be negative			
			GlobalClass.cargoTrain.deriveAccel = (cargoTrainData.aDer.getTextAsNumber() < 0 ? cargoTrainData.aDer.getTextAsNumber() : cargoTrainData.aDer.getTextAsNumber()*(-1)); //Must be Negative			
			//Save passenger train data
			GlobalClass.passengerTrain.trainVMax = passengerTrainData.vMax.getTextAsNumber() / 3.6;
			GlobalClass.passengerTrain.accel = passengerTrainData.acel.getTextAsNumber();
			GlobalClass.passengerTrain.brakeAccel = (passengerTrainData.aFren.getTextAsNumber() < 0 ? passengerTrainData.aFren.getTextAsNumber() : passengerTrainData.aFren.getTextAsNumber()*(-1)) ; //Must be negative
			GlobalClass.passengerTrain.deriveAccel = (passengerTrainData.aDer.getTextAsNumber() < 0 ? passengerTrainData.aDer.getTextAsNumber() : passengerTrainData.aDer.getTextAsNumber()*(-1)); //Must be Negative			
		}
		
		private function initTrains() : void
		{
			GlobalClass.cargoTrain.trainVMax = 0;
			GlobalClass.cargoTrain.accel = 0;
			GlobalClass.cargoTrain.brakeAccel = 0 ;		
			GlobalClass.cargoTrain.deriveAccel = 0; 		
			
			GlobalClass.passengerTrain.trainVMax = 0;
			GlobalClass.passengerTrain.accel = 0;
			GlobalClass.passengerTrain.brakeAccel = 0;
			GlobalClass.passengerTrain.deriveAccel = 0;
		}
	}
}